package base.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;


/**
 * @author 3
 * @date  3
 */
@Controller
public class UserController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @RequestMapping("getImgs")
    @ResponseBody
    private void getScreenImgs(HttpServletResponse response) throws Exception {
            System.out.println("begin---");
            Thread.sleep(1);
            response.setContentType("image/jpg");
            ServletOutputStream sos = response.getOutputStream();
            // 禁止页面缓存
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            // 创建内存图象并获得其图形上下文
            Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
            BufferedImage screenshot = (new Robot())
                    .createScreenCapture(new Rectangle(0, 0, (int) dimension
                            .getWidth(), (int) dimension.getHeight()));
            // 将图像输出到客户端
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageIO.write(screenshot, "jpg", bos);
            byte[] buf = bos.toByteArray();
            response.setContentLength(buf.length);
            sos.write(buf);
            bos.close();
            sos.close();
    }

    @RequestMapping("getObj")
    @ResponseBody
    public String register(){
        Map<String, String> obj = new HashMap();
        obj.put("name", "feiji");
        obj.put("password", "getObj");
        System.out.println(obj);
        return JSON.toJSONString(obj);
    }

    @RequestMapping("setCookie")
    @ResponseBody
    public String setCookie(){
        Map<String, String> obj = new HashMap();
        obj.put("name", "feiji");
        obj.put("password", "setCookie");
        System.out.println(obj);
        return JSON.toJSONString(obj);
    }
    @RequestMapping("removeCookie")
    @ResponseBody
    public String removeCookie(HttpServletResponse response, HttpServletRequest request){
        Map<String, String> obj = new HashMap();
        System.out.println(obj);
        for (Cookie cookie : request.getCookies()) {
            cookie.setMaxAge(0);
            cookie.setPath("/learn");
            response.addCookie(cookie);
        }
        return JSON.toJSONString(obj);
    }

    @RequestMapping("other")
    @ResponseBody
    public JSONObject fff(){
        Map<String, String> obj = new HashMap();
        obj.put("name", "feiji");
        obj.put("password", "otherfdsafa防守打法");

        System.out.println(obj);
        logger.info("hh-info");
        System.out.println(0/0);
        return JSON.parseObject(obj.toString());
    }

}
